package com.vinaprime.primeschedule.presentation;

import java.text.ParseException;
import java.util.List;

import android.app.ActionBar;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.vinaprime.primeschedule.businesslayer.VPGroupHeader_BL;
import com.vinaprime.primeschedule.entities.VPGroupHeader;

public class VPContactGroupActivity extends Activity {

	public final static String CONTACT_GROUP_ID = "com.vinaprime.primeschedule.presentation.MESSAGE";
	final private static int DIALOG_LOGIN = 1;
	int _idConntactGroup;
	int _position;
	String  _StringId;
	List<VPGroupHeader> listContactGroup;
	List<VPGroupHeader> listContactGroupDelete;
	List<VPGroupHeader> listContactGroupFilter;
    ListView listView;
    ListViewContactGroupAdapter list_adapter;
    VPGroupHeader_BL contactGroupBL;
    ApplicationInformation appInfo;
    FrameLayout frameControl;
    
    String path;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_vpcontact_group);
		ActionBar actionBar = getActionBar();
		Drawable d=getResources().getDrawable(R.raw.background_top);  
		actionBar.setBackgroundDrawable(d);
		//---------------------------------//
		frameControl = (FrameLayout)findViewById(R.id.frame_control);
		frameControl.setVisibility(View.INVISIBLE);
		//---------------------------------//
		appInfo = new ApplicationInformation(this);
		path = appInfo.getDBPath();
		contactGroupBL = new VPGroupHeader_BL(this,  path + "databases/");
	
		showListItem();
	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		
		if(requestCode == 8 )
		{
			if(null!=data)
			{
				showListItem();
			}
		}
		
		
	};
	public void showListItem()
	{	 
		try {
			
			listContactGroup = contactGroupBL.getAllGroupHeader();
			//Toast.makeText(getBaseContext(),  "  111  --- " + listContactGroup.size(), Toast.LENGTH_SHORT).show();
			
			listView = (ListView) findViewById(R.id.listView_contactGroup);
			list_adapter = new  ListViewContactGroupAdapter(listContactGroup,this);
			listView.setAdapter(list_adapter);
			
		
			listView.setOnItemClickListener(new OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> arg0, View arg1,
						int position, long arg3) {					
				//Toast.makeText(getBaseContext(), listContactGroup.get(position).getVpghId()+  "  222", Toast.LENGTH_SHORT).show();
					if( !String.valueOf(listContactGroup.get(position).getVpghId()).isEmpty()){
						_idConntactGroup = listContactGroup.get(position).getVpghId();
						_position = position;
						Intent intent = new Intent(getApplicationContext(), VPContactGroupDetailsActivity.class);
					//intent.putExtra(CONTACT_ID, String.valueOf(listContact.get(arg2).getVpcID()));
					intent.putExtra(CONTACT_GROUP_ID, new String[]{String.valueOf(_idConntactGroup)});
					startActivityForResult(intent,8);
					}
					
				}
				
			});
			listView.setOnItemLongClickListener(new OnItemLongClickListener() {

				@Override
				public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
						int position, long arg3) {
					/*
					_idConntactGroup = listContactGroup.get(position).getVpghId();
					_position = position;
					registerForContextMenu(listView);
					return false;*/
					/*
					//Mutiselect listview
					listContactGroupDelete = null;
					listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
					listView.setOnItemClickListener(new OnItemClickListener() {

						@Override
						public void onItemClick(AdapterView<?> arg0, View arg1,
								int arg2, long arg3) {
							// TODO Auto-generated method stub
							listContactGroupDelete.add(listContactGroup.get(arg2));
						}
					
					});
					//Show frame
					frameControl.setVisibility(View.VISIBLE);
					Button _btnDelete = (Button)frameControl.findViewById(R.id.frame_button_delete);
					_btnDelete.setOnClickListener(new OnClickListener() {
						
						@Override
						public void onClick(View arg0) {
							// TODO Auto-generated method stub
							Toast.makeText(getApplicationContext(), "Delete" + listContactGroupDelete.size() , Toast.LENGTH_LONG).show();
						}
					});
					
					Button _btnCancel = (Button)frameControl.findViewById(R.id.frame_button_cancel);
					_btnCancel.setOnClickListener(new OnClickListener() {
						
						@Override
						public void onClick(View arg0) {
							// TODO Auto-generated method stub
							frameControl.setVisibility(View.INVISIBLE);
						}
					});
					*/
					return true;
				}
			});
			
			
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
		} 
		
		catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			//Toast.makeText(getBaseContext(), e.toString() + "  aaa", Toast.LENGTH_SHORT).show();
		}
		
	}

	@Override
	  public boolean onOptionsItemSelected(MenuItem item) {
	    switch (item.getItemId()) {
	    case R.id.action_add:
	    	
	    	showDialogAddGroup();
	      break;

	    default:
	    	break;
	    }

	    return true;
	  } 
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.vpcontact_group, menu);
		return true;
	}
	public void showDialogAddGroup()
	{
		LayoutInflater inflater = (LayoutInflater) this
		        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View textEntryView  = inflater.inflate(R.layout.dialog_add_group_contact, null);

        AlertDialog.Builder alert = new AlertDialog.Builder(this); 

        alert.setTitle("Add new a groups"); 
        alert.setMessage("Enter your name and description."); 
        // Set an EditText view to get user input  
        alert.setView(textEntryView); 
        
        
        AlertDialog addGroupView = alert.create();

        final EditText txtGroupName = (EditText) textEntryView.findViewById(R.id.editText_GroupName);
        final EditText txtGroupDesc = (EditText) textEntryView.findViewById(R.id.editText_DescName);
        
        alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				//Insert contactgroupheader
				VPGroupHeader gh = new VPGroupHeader(
						contactGroupBL.getNewGroupHeaderID(), 
						"", 
						txtGroupName.getText().toString(), 
						txtGroupDesc.getText().toString(), 
						"Y");
				contactGroupBL.addGroupHeader(gh);
				//refresh listView
				listContactGroup.add(0,gh);
				list_adapter.notifyDataSetChanged();
				listView.setAdapter(list_adapter);
				listView.invalidateViews();
				
			}
		});
        
        alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				return;
			}
		});
        alert.show(); 
	}
	
	private class ListViewContactGroupAdapter extends BaseAdapter {
		 

	    Context context;
	    List<VPGroupHeader> listContactGroup;
	    public ListViewContactGroupAdapter(List<VPGroupHeader> _listContactGroup,Context _context) {
	    	listContactGroup = _listContactGroup;
	        context=_context;
	    }
	    @Override
	    public int getCount() {
	 
	        return listContactGroup.size();
	    }

	    @Override
	    public Object getItem(int position) {
	        // TODO Auto-generated method stub
	       return listContactGroup.get(position);
	    }
	 
	    @Override
	    public long getItemId(int position) {
	        // TODO Auto-generated method stub
	    	return listContactGroup.get(position).getVpghId();
	    	 
	    }
	 
	    @Override
	    public View getView(int position, View convertView, ViewGroup parent) {
	 
	        // create a new LayoutInflater
	        LayoutInflater inflater = (LayoutInflater) context
	                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	 
	        View listView;
	        listView = null;
	        convertView = null;// avoids recycling of grid view
	        if (convertView == null) {
	 
	        	listView = new View(context);
	            // inflating list view item
	        	listView = inflater.inflate(R.layout.activity_vpcontact_group_item, null);
	        	
	        	//Tao VPGroupHeader
	        	VPGroupHeader _vpContactGroup = new VPGroupHeader();
	        	_vpContactGroup = (VPGroupHeader)getItem(position);
	        	//set value into textview id
	        	
	            TextView textViewId = (TextView) listView.findViewById(R.id.textView_contactGroupID);
	            textViewId.setText(String.valueOf(_vpContactGroup.getVpghId()));
	            
	            // set value into textview
	            TextView textViewName = (TextView) listView.findViewById(R.id.textView_contactGroupName);
	            textViewName.setText(_vpContactGroup.getVpghName());
	         // set value into textview
	            TextView textViewDesc = (TextView) listView.findViewById(R.id.textView_contactGroupDesc);
	            textViewDesc.setText(_vpContactGroup.getVpghDescription());
	                        			
	        }
	 
	        return listView;
	    }
	}
}
